package com.wilson.integration.lease.admin.controller.user;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wilson.integration.lease.admin.service.UserInfoService;
import com.wilson.integration.lease.common.result.Result;
import com.wilson.integration.lease.common.entity.UserInfo;
import com.wilson.integration.lease.common.enums.BaseStatus;
import com.wilson.integration.lease.common.entity.vo.user.UserInfoQueryVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户信息管理")
@RestController
@RequestMapping("/admin/user")
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;

    @Operation(summary = "分页查询用户信息")
    @GetMapping("page")
    public Result<IPage<UserInfo>> pageUserInfo(@RequestParam long current, @RequestParam long size, UserInfoQueryVO queryVO) {
        IPage<UserInfo> page = new Page<>(current, size);
        IPage<UserInfo> list = userInfoService.page(page,
                new LambdaQueryWrapper<UserInfo>()
                        .like(ObjectUtil.isNotNull(queryVO.getPhone()), UserInfo::getPhone, queryVO.getPhone())
                        .eq(ObjectUtil.isNotNull(queryVO.getStatus()), UserInfo::getStatus, queryVO.getStatus()));
        return Result.ok(list);
    }

    @Operation(summary = "根据用户id更新账号状态")
    @PostMapping("updateStatusById")
    public Result updateStatusById(@RequestParam Long id, @RequestParam BaseStatus status) {
        userInfoService.update(
                new LambdaUpdateWrapper<UserInfo>()
                        .eq(UserInfo::getId, id)
                        .set(UserInfo::getStatus, status));
        return Result.ok();
    }
}
